//
//  StatusRetweetedView.swift
//  sinaweibowj
//
//  Created by Macintosh on 15/12/21.
//  Copyright © 2015年 mactoinsh. All rights reserved.
//

import UIKit
import SnapKit

//转发微博：微博正文+配图视图
class StatusRetweetedView: UIView {

    //定义约束属性，保存当前视图的底部约束
    var bottomConstraints:Constraint?
    
    //MARK:定义外部模型属性
    var retweetedStatus:Status?{
        didSet{
        textLabel.text = "@" + (retweetedStatus?.user?.name ?? "") + (retweetedStatus?.text ?? "")
        pictureView.imageURL = retweetedStatus?.imageURL
            
            //卸掉之前的约束
            self.bottomConstraints?.uninstall()
            
            //根据是否有配图，重新添加约束
            if let urls = retweetedStatus?.imageURL where urls.count > 0{
            //有配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
                })
            } else {
            //没有配图
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.bottomConstraints = make.bottom.equalTo(textLabel.snp_bottom).offset(StatusCellMarigin).constraint
            })
            
            }
            
            
            
        }
    }
    
    //MARK:重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:设置页面和布局
    private func setupUI(){
        
        addSubview(textLabel)
        addSubview(pictureView)
        
        //添加约束
        textLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMarigin)
            
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(textLabel.snp_left)
        }
        
        //自动计算行高
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
        }
        
    }
    
    //MARK:懒加载所有的子视图
    //微博正文
    private lazy var textLabel: UILabel = UILabel(title: "我是转发微博", color: UIColor.darkGrayColor(), fontSize: 14, margin: 0)
    //配图视图
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}
